/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} limit
 * @return {TreeNode}
 */
var sufficientSubset = function (root, limit) {
  function dfs(sum, node) {
    if (!node) {
      return false
    }

    if (node.left == null && node.right == null) {
      return sum + node.val >= limit
    }
    const left = dfs(sum + node.val, node.left)
    if (!left) {
      node.left = null
    }
    const right = dfs(sum + node.val, node.right)
    if (!right) {
      node.right = null
    }
    return left || right

  }

  const res = dfs(0, root)
  return res ? root : null;

};